#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
ll a[N], b[N];
ll extgcd(ll a, ll b, ll& x, ll& y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
void merge(int i) {
  if (a[i] > a[i + 1]) swap(a[i], a[i + 1]), swap(b[i], b[i + 1]);
  ll c = a[i + 1] - a[i];  // b[i] * ki + a[i] = b[i + 1] * ki+1 + a[i + 1]
  ll m1 = b[i], m2 = b[i + 1];
  ll x, y;
  ll d = extgcd(m1, m2, x, y);
  if (c % d) {
    cout << -1;
    exit(0);
  }
  c /= d, m1 /= d, m2 /= d;
  // (x * c + m2 * k) * b[i] + a[i]
  ll m = m2 * b[i];
  ll r = ((__int128_t)x * c % m * b[i] + a[i]) % m;
  if (r < 0) r += m;
  a[i + 1] = r, b[i + 1] = m;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> b[i] >> a[i];
  rep(i, 1, n - 1) merge(i);
  cout << a[n];
  return 0;
}